package com.sxygsj.android.personnelscreen.plugin_jiguang.advertisement

import android.os.Bundle
import android.os.Environment
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.VideoView
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.lzy.okgo.utils.HttpUtils.runOnUiThread
import com.sxygsj.android.personnelscreen.plugin_jiguang.R
import com.tencent.mmkv.MMKV
import java.io.File

class AdvertisementFragment : Fragment() {
    lateinit var imageView: ImageView
    var playVideoName = "video_1"
    lateinit    var advertisement_vv_video:VideoView
    val videoArrayList=ArrayList<String>()
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_advertisement, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        imageView = view.findViewById(R.id.advertisement_iv_image)
        videoArrayList.add(
            "${Environment.getExternalStorageDirectory()}/com.sxygsj.android.personnelscreen.advertisement/video_1.mp4"
            )
        videoArrayList.add(
            "${Environment.getExternalStorageDirectory()}/com.sxygsj.android.personnelscreen.advertisement/video_2.mp4")
        videoArrayList.add(
            "${Environment.getExternalStorageDirectory()}/com.sxygsj.android.personnelscreen.advertisement/video_3.mp4")

         advertisement_vv_video= view.findViewById(R.id.advertisement_vv_video)

        advertisement_vv_video.setOnCompletionListener {
            when(playVideoName){
                "video_1"->{
                    advertisement_vv_video.setVideoPath(videoArrayList[1])
                    playVideoName="video_2"
                }
                "video_2"->{
                    advertisement_vv_video.setVideoPath(videoArrayList[2])
                    playVideoName="video_3"
                }
                "video_3"->{
                    advertisement_vv_video.setVideoPath(videoArrayList[0])
                    playVideoName="video_1"
                }
            }
            advertisement_vv_video.start()
        }
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val mmkv = MMKV.mmkvWithID("advertisementFile")!!
        var filePath = mmkv.decodeString("file", "")
        if (TextUtils.isEmpty(filePath)) {
            filePath="${Environment.getExternalStorageDirectory()}/com.sxygsj.android.personnelscreen.advertisement/video_1.mp4"
            val video1 = File(filePath)
            if (video1.exists()) {
                imageView.visibility = View.GONE
                playVideoName=video1.name
                advertisement_vv_video.visibility=View.VISIBLE
                advertisement_vv_video.setVideoPath(filePath)
                advertisement_vv_video.start()
            }else{
                imageView.visibility = View.VISIBLE
                advertisement_vv_video.visibility=View.GONE
                Glide.with(this).load(R.drawable.main_recycler_item_bg).into(imageView)
            }

        } else {
            val file = File(filePath)
            playVideoName = file.name
            val fileType: String = playVideoName.substring(playVideoName.lastIndexOf(".") + 1, playVideoName.length).toLowerCase()
            runOnUiThread {
                if (fileType == "jpg" || fileType == "png") {
                    imageView.visibility = View.VISIBLE
                    advertisement_vv_video.visibility=View.GONE
                    Glide.with(this).load(file).into(imageView)
                } else if (fileType == "mp4") {
                  advertisement_vv_video.visibility=View.VISIBLE
                    imageView.visibility = View.GONE
                    advertisement_vv_video.setVideoPath(filePath)
                }
            }


        }
    }
    override fun onResume() {
        super.onResume()
        advertisement_vv_video.start()
    }

    override fun onPause() {
        super.onPause()
        advertisement_vv_video.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        advertisement_vv_video.stopPlayback()
    }

}